package com.homework.HomeAcount.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.homework.HomeAcount.dao.UserDao;
import com.homework.HomeAcount.entity.User;
import com.homework.HomeAcount.service.UserService;
import com.homework.HomeAcount.util.MD5Util;

@CacheConfig(cacheNames="user")
@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	
	public User getUserInfo(User user) {
		return userDao.login(user);
	}
	
	@Override
	public User findUserByUserName(String userName) {
		List<User> users = userDao.findUserByUserName(userName);
		return null != users && users.size() > 0? users.get(0) : null;
	}
	
	@Cacheable//使用默认的缓存key
	@Override
	public List<User> loadUserList(Map<String, Object> map) {
		return userDao.loadUserList(map);
	}

	@Override
	public int getUserListSize(Map<String, Object> map) {
		return userDao.getUserListSize(map);
	}

	@Override
	public User loadUserInfoById(String id) {
		return userDao.loadUserInfoById(id);
	}

	@Override
	public void addUser(User user) throws Exception {
		MD5Util.encodePassword(user);
		userDao.addUser(user);
	}

	@Override
	public void updateUser(User user) throws Exception {
		userDao.updateUser(user);
	}

	@Override
	public void deleteUser(String id) throws Exception {
		userDao.deleteUser(id);
	}

}
